import { getDataByStatusOrType, getPublishContest } from "@/api/contest";
import { GetActivityRes } from "@/types/events";
import { onMounted, ref } from "vue";
import { getPublishActivity } from "@/api/events";
import { useRoute } from "vue-router";
/**
 * 获取 个人中心页面的竞赛活动数据
 */

export function useMyData() {
    const data = ref<GetActivityRes[]>([]);
    const route = useRoute();
    const loading = ref(false);
    // 获取我发布的
    async function getAddData(): Promise<GetActivityRes[]> {
        let data;
        loading.value = true;
        if (route.name === "我的竞赛") {
            // 竞赛
            data = await getPublishContest();
        } else {
            // 活动
            data = await getPublishActivity();
        }
        loading.value = false;
        return data.data.data;
    }
    // 获取报名或提交的数据
    async function getData(status: string, type: string) {
        loading.value = true;
        const data = await getDataByStatusOrType(status, type);
        loading.value = false;
        return data.data.data;
    }
    onMounted(async () => {
        // 我发布的
        data.value = await getAddData();
    });

    return {
        data,
        getData,
        getAddData,
        loading,
    };
}
